package com.cocola.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cocola.mapper.DeclarationMapper;
import com.cocola.pojo.Declaration;
import com.cocola.pojo.Unit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DeclarationService {

    @Autowired
    private DeclarationMapper declarationMapper;


    public List<Declaration> findDeclaration() {
        return declarationMapper.selectList(null);
    }

    /*
    **查找条件匹配 unitSend 的全部
     */
    public List<Declaration> findDeclaration2(String unitSend) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("unit_send", unitSend);

        return declarationMapper.selectByMap(map);
    }

    /*
     **查找条件匹配 unitName 的全部
     */
    public List<Declaration> findOneDeclaration(String unitName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("unit_name", unitName);

        return declarationMapper.selectByMap(map);
    }

    //tips: List不用比较运算符
    /*
     **查找条件匹配 unitSend，major，userName 的全部
     */
    public boolean findForAdd(String unitSend, String major, String userName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("unit_send", unitSend);
        map.put("major", major);
        map.put("user_name", userName);
        List<Declaration> declarationList = declarationMapper.selectByMap(map);

        if(declarationList.isEmpty()) {
            return true;
        }else {
            return false;
        }

    }


    /*
    **insert
     */
    public void add(String unitName, String userType, String unitSend, String job, String major, String userName, String nation,
                    String idCard, String political, String birthTime, String foreignLanguage, String sex,
                    String special, String nativePlace, String birthPlace, String award, String phone,
                    String clubJob, String fileLocation, String photo) {

        Declaration declaration = new Declaration();
        declaration.setUnitName(unitName);
        declaration.setUserType(userType);
        declaration.setUnitSend(unitSend);
        declaration.setJob(job);
        declaration.setMajor(major);
        declaration.setUserName(userName);
        declaration.setNation(nation);
        declaration.setIdCard(idCard);
        declaration.setPolitical(political);
        declaration.setBirthTime(birthTime);
        declaration.setForeignLanguage(foreignLanguage);
        declaration.setSex(sex);
        declaration.setSpecial(special);
        declaration.setNativePlace(nativePlace);
        declaration.setBirthPlace(birthPlace);
        declaration.setAward(award);
        declaration.setPhone(phone);
        declaration.setClubJob(clubJob);
        declaration.setFileLocation(fileLocation);
        declaration.setPhoto(photo);

        int insert = declarationMapper.insert(declaration);

    }

    /*
    **update
     */
    public void update(String userName, String nation, String idCard, String political, String birthTime,
                       String foreignLanguage, String sex, String special, String nativePlace, String birthPlace,
                       String award, String phone, String clubJob, String fileLocation, String photo) {

        Declaration declaration = new Declaration();
        declaration.setUserName(userName);
        declaration.setNation(nation);
        declaration.setIdCard(idCard);
        declaration.setPolitical(political);
        declaration.setBirthTime(birthTime);
        declaration.setForeignLanguage(foreignLanguage);
        declaration.setSex(sex);
        declaration.setSpecial(special);
        declaration.setNativePlace(nativePlace);
        declaration.setBirthPlace(birthPlace);
        declaration.setAward(award);
        declaration.setPhone(phone);
        declaration.setClubJob(clubJob);
        declaration.setFileLocation(fileLocation);
        declaration.setPhoto(photo);

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("user_name", userName);
        declarationMapper.update(declaration, updateWrapper);

    }


    /*
    **update status，匹配条件 unitSend
     */
    public void updateStatus(String status, String unitSend) {

        Declaration declaration = new Declaration();
        declaration.setStatus(status);
        QueryWrapper<Declaration> wrapper = new QueryWrapper<>();
        wrapper.eq("unit_send", unitSend);
        declarationMapper.update(declaration, wrapper);
    }


}
